package com.example.swd1.gudong.main;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.example.swd1.gudong.view.TabView;

import java.util.List;

//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-24
//   Role :    Adapter for fragments in MainActivity
//   Reference documents : None
//==================================================================================================================
public class MainFragmentTabAdapter implements TabView.BottomPanelCallback {

    private List<Fragment> fragments;
    private TabView tabView;
    private FragmentActivity fragmentActivity;
    private int fragmentContentId; //the id of the view that show the fragment
    private int currentTab;
    private OnTabChangedListener onTabChangedListener;

    public MainFragmentTabAdapter(FragmentActivity activity, List<Fragment> fragments, int fragmentContentId, TabView tabView) {
        this.fragments = fragments;
        this.tabView = tabView;
        this.fragmentActivity = activity;
        this.fragmentContentId = fragmentContentId;

        // default display the sport fragment
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
        ft.add(fragmentContentId,fragments.get(2));
        ft.commit();
        tabView.setBottomCallback(this);
    }

    @Override
    public void onBottomPanelClick(int itemId) {
        Fragment fragment = fragments.get(itemId);
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
        getCurrentFragment().onPause(); // pause current tab
        if (fragment.isAdded()) {
            fragment.onResume(); // resume next tab
        } else {
            ft.add(fragmentContentId, fragment);
        }
        showTab(itemId);
        ft.commit();
        if (null != onTabChangedListener) {
            onTabChangedListener.OnTabCheckedChanged(tabView, itemId);
        }
    }
    /**
     * change tab
     */
    private void showTab(int index) {
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
            if (index == i) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commit();
        }
        currentTab = index;
    }

    public int getCurrentTab() {
        return currentTab;
    }

    public Fragment getCurrentFragment() {
        return fragments.get(currentTab);
    }

    public OnTabChangedListener getOnRgsExtraCheckedChangedListener() {
        return onTabChangedListener;
    }

    public void setOnTabChangedListener(OnTabChangedListener onTabChangedListener) {
        this.onTabChangedListener = onTabChangedListener;
    }

    public interface OnTabChangedListener {
        void OnTabCheckedChanged(TabView tabView,int index);
    }
}
